"""操作txt文件模块"""

import os

from .paths import FILE_ONE_PATH, FILE_TWO_PATH


class TxtControl(object):

    @staticmethod
    def get_text(product_dict):
        """..."""

        txt_list = os.listdir(FILE_ONE_PATH)
        flag1, flag2_list, message_dict = False, [], {}

        for i, (product_info, product_num) in enumerate(product_dict.items(), start=1):
            temp_flag = True
            flag = True if '??' in product_info else False

            for txt in txt_list:
                new_txt = txt.split('.txt')[0]
                has_plus = '+' in new_txt

                if flag is False and not has_plus and new_txt in product_info:
                    print(f'Match successfully:{product_info} ---> {txt}')
                    flag2, secret_list = TxtControl.get_secret(new_txt, product_num)
                    flag2_list.append(flag2)
                    message_dict.setdefault(new_txt, secret_list)
                    flag1, temp_flag = True, False
                    break

                if flag is True and has_plus and new_txt.split('+')[0] in product_info and new_txt.split('+')[
                    1] in product_info:
                    print(f'Match successfully:{product_info} ---> {txt}')
                    flag2, secret_list = TxtControl.get_secret(new_txt, product_num)
                    flag2_list.append(flag2)
                    message_dict.setdefault(new_txt, secret_list)
                    flag1, temp_flag = True, False
                    break

            if temp_flag:
                print(f'Matching failed:"{product_info}" No matching TXT file exists')

        if flag1 and True in flag2_list:
            return message_dict

        return False

    @staticmethod
    def get_secret(new_txt, product_num):
        """获取卡密"""

        product_num = int(product_num.strip('x'))
        all_file = FILE_ONE_PATH + '/' + new_txt + '.txt'
        secret_list = []

        with open(all_file, 'r', encoding='utf-8') as f:
            lines1 = f.readlines()
            if len(lines1) >= product_num:
                for i in range(product_num):
                    secret_list.append(lines1[i].strip())

        if len(secret_list):
            print(f'Card secret obtained:{"、".join(secret_list)}')
            return True, secret_list

        print(f'{new_txt}.txt. The number of card secrets in the file is insufficient, please add it in time!')

        return False, ['in short supply']

    @staticmethod
    def delete_secret(message_dict):
        """删除卡密"""

        for new_txt, secret_list in message_dict.items():
            all_file = FILE_ONE_PATH + '/' + new_txt + '.txt'
            used_file = FILE_TWO_PATH + '/' + new_txt + '.txt'

            with open(all_file, 'r', encoding='utf-8') as f:
                lines = f.readlines()

            with open(all_file, 'w', encoding='utf-8') as f:
                for line in lines:
                    if line.strip() not in secret_list:
                        f.write(line)

            with open(used_file, 'a', encoding='utf-8') as f:
                for item in secret_list:
                    f.write(item + '\n')

            print(f'Card secrets removed:{"、".join(secret_list)}')
